import { useParams } from 'react-router'
import dayjs from 'dayjs'

import { EventEmitter } from '@/utils/eventEmitter'
import airplane from '@/assets/icons/airplane.svg?react'
import railwayFreight from '@/assets/icons/railwayFreight.svg?react'
import seaFreight from '@/assets/icons/seaFreight.svg?react'
import trainFreight from '@/assets/icons/trainFreight.svg?react'

export const formatTime = (time?: string | number | Date) => {
  if (!time) return '-'
  return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
}

export type TEventArgsMap = {
  /** 转单号变更，如录入转单号弹窗操作成功 */
  ['transferOrderNumChanged']: []
  /** 附件变更，如：发布路由所上传的附件、留言所上传/删除的附件 */
  ['attachmentChanged']: []
}
export const eventBus = new EventEmitter<TEventArgsMap>()

export const useUrlParams = () => {
  const waybillNo = useParams().waybillNo || ''
  return {
    /** 运单号 */
    waybillNo,
  }
}

export const COMMENT_ID = 'comment'

export const getWaybillStatusName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 11:
        return '待处理' as const
      case 12:
        return '待发货' as const
      case 13:
        return '待离港' as const
      case 14:
        return '运输中' as const
      case 15:
        return '待提取' as const
      case 16:
        return '中转中' as const
      case 17:
        return '派送中' as const
      case 18:
        return '已签收' as const
      case 19:
        return '已取消' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [11, 12, 13, 14, 15, 16, 17, 18, 19] },
  {
    colorMap: {
      11: 'magenta',
      12: 'cyan',
      13: 'gold',
      14: 'orange',
      15: 'green',
      16: 'blue',
      17: 'purple',
      18: 'red',
      19: 'gray',
    } as Record<11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19, string>,
    colorOf: (value?: number | null) =>
      value != null ? getWaybillStatusName.colorMap[value as 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19] : undefined,
  },
)

export const getCustomsExportStatusName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 31:
        return '报关中' as const
      case 32:
        return '已报关' as const
      case 33:
        return '报关异常' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [31, 32, 33] },
)

export const getCustomsImportStatusName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 41:
        return '清关中' as const
      case 42:
        return '已清关' as const
      case 43:
        return '清关异常' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [41, 42, 43] },
)

export const getAppointmentStatusName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 51:
        return '已预约' as const
      case 52:
        return '预约变更' as const
      case 53:
        return '预约取消' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [51, 52, 53] },
)

export const getInsuranceStatusName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 61:
        return '已保险' as const
      case 62:
        return '保险变更' as const
      case 63:
        return '保险取消' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [61, 62, 63] },
)

export const getStaffTypeName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 1:
        return '业务人员' as const
      case 2:
        return '港后跟单' as const
      case 3:
        return '港前操作' as const
      case 4:
        return '港后操作' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [1, 2, 3, 4] },
)

export const getFirstLineName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 1:
        return 'OCEAN' as const
      case 2:
        return 'AIR' as const
      case 3:
        return 'GROUND_TRUCK' as const
      case 4:
        return 'GROUND_TRAIN' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [1, 2, 3, 4] },
  {
    colorMap: {
      1: 'magenta',
      2: 'cyan',
      3: 'gold',
      4: 'lime',
    } as Record<1 | 2 | 3 | 4, string>,
    colorOf: (value?: number | null) => (value != null ? getFirstLineName.colorMap[value as 1 | 2 | 3 | 4] : undefined),
  },
)

export const getTailLineName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 1:
        return '快递' as const
      case 2:
        return '卡车' as const
      case 3:
        return 'Amazon Freight' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [1, 2, 3] },
  {
    colorMap: {
      1: 'magenta',
      2: 'cyan',
      3: 'gold',
    } as Record<1 | 2 | 3, string>,
    colorOf: (value?: number | null) => (value != null ? getTailLineName.colorMap[value as 1 | 2 | 3] : undefined),
  },
)

export const getTransportationTypeName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 1:
        return '海运' as const
      case 2:
        return '空运' as const
      case 3:
        return '卡车' as const
      case 4:
        return '铁路' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [1, 2, 3, 4] },
  {
    iconMap: {
      1: seaFreight,
      2: airplane,
      3: trainFreight,
      4: railwayFreight,
    } as Record<1 | 2 | 3 | 4, React.ComponentType<React.SVGProps<SVGSVGElement>>>,
  },
)

export const getLogisticsProductServiceLevelName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 1:
        return 'Standard' as const
      case 2:
        return 'Fast' as const
      case 3:
        return 'Expedited' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [1, 2, 3] },
)

export const getLastTransportationTypeName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 1:
        return '快递' as const
      case 2:
        return '卡车' as const
      case 3:
        return 'Amazon Freight' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  {
    valueList: [1, 2, 3] as const,
    iconMap: { 1: airplane, 2: railwayFreight, 3: railwayFreight } as Record<
      1 | 2 | 3,
      React.ComponentType<React.SVGProps<SVGSVGElement>>
    >,
  },
)

export const getLocationTypeName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 1:
        return '始发地' as const
      case 2:
        return '集货仓' as const
      case 3:
        return '起运港' as const
      case 4:
        return '目的港' as const
      case 5:
        return '中转仓' as const
      case 6:
        return '目的地' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [1, 2, 3, 4, 5, 6] },
)

/** 查询类型枚举 */
export const getFileTypeName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      // 1-装箱单 2-报关文件 3-清关文件 4-标签文件 5-签收文件(POD) 6-其他文件 7-ISA 8-BL
      case 1:
        return '装箱单' as const
      case 2:
        return '报关文件' as const
      case 3:
        return '清关文件' as const
      case 4:
        return '标签文件' as const
      case 5:
        return '签收文件(POD)' as const
      case 6:
        return '其他文件' as const
      case 7:
        return 'ISA' as const
      case 8:
        return 'BL' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [1, 2, 3, 4, 5, 6, 7, 8] },
)
